package com.es.elasticsearch.util;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class ESIndexUtil {

   static String host = "172.16.87.140";

    /**
     * 建立连接
     * @return
     */
    public static RestHighLevelClient getESInstance()
    {

        HttpHost httpHost = new HttpHost(host, 9200, "http");

        RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
        return client;
    }

    /**
     * 关闭
     * @param client
     */
    public static void closeIns(RestHighLevelClient client)
    {
        // 关闭客户端连接
        try {
            if(client != null)
            {
                client.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建索引
     * @param indexName
     * @throws IOException
     */
    public static String createIndex(String indexName) throws IOException {
        RestHighLevelClient client = ESIndexUtil.getESInstance();

        CreateIndexRequest request = new CreateIndexRequest(indexName);
        // 发送请求，获取响应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);

        boolean acknowledged = response.isAcknowledged();
        // 响应状态
        System.out.println("操作状态 = " + acknowledged);


        client.close();

        return acknowledged+"";
    }


    /**
     * 查询索引
     * @param indexName
     * @return
     * @throws IOException
     */
    public static String queryIndex(String indexName) throws IOException {

        RestHighLevelClient client = ESIndexUtil.getESInstance();

        // 查询索引 - 请求对象
        GetIndexRequest request = new GetIndexRequest(indexName);

        // 发送请求，获取响应
        GetIndexResponse response = client.indices().get(request,
                RequestOptions.DEFAULT);
        System.out.println("aliases:" + response.getAliases());
        System.out.println("mappings:" + response.getMappings());
        System.out.println("settings:" + response.getSettings());

        return response.getAliases() + "\n"
                + response.getMappings() + "\n"
                + response.getSettings() + "\n";
    }


    /**
     * 删除索引
     * @param indexName
     * @return
     */
    public static String deleteIndex(String indexName) throws IOException {
        RestHighLevelClient client = ESIndexUtil.getESInstance();

        // 删除索引 - 请求对象
        DeleteIndexRequest request = new DeleteIndexRequest(indexName);
        // 发送请求，获取响应
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        // 操作结果
        System.out.println("操作结果 ： " + response.isAcknowledged());

        return response.isAcknowledged() + "";
    }


}
